<template>
	<view>
		<view class="search">
			<view class="left">
				<uni-search-bar radius="15" v-model="keywords" :placeholder="placeholderText" clearButton="auto"
					cancelButton="none" @confirm="searchConfirm" @clear="searchClear" @input="searchInput"
					cancelText="搜索" />
			</view>
			<view class="right" @click="searchClick">
				搜索
			</view>
		</view>

		<template v-if="!isSearch">
			<view class="history" v-if="historySearch.length > 0">
				<view class="header">
					<view class="text">
						历史搜索
					</view>
					<view class="clear">
						<view v-if="!isClear" @click="clearupClick">
							清除
						</view>
						<view class="clear-item" v-else>
							<view @click="clearAll">
								全部清除
							</view>
							<view class="line">

							</view>
							<view class="complete" @click="clearupClick">
								完成
							</view>
						</view>
					</view>
				</view>
				<view class="tags">
					<view class="tag-item" v-for="(item,index) in historySearch" :key="index"
						@click="keywrodsClick(item)">
						<view class="tag">
							{{ item }}
						</view>
						<view class="close" @click.stop="clearKeywords(index)" v-if="isClear">
							<uni-icons type="closeempty" size="16" color="#000"></uni-icons>
						</view>
					</view>
				</view>
			</view>

			<view class="hot">
				<view class="header">
					热门搜索
				</view>
				<view class="tags">
					<view class="tag" v-for="(item,index) in hotSearchList" :key="index" @click="keywrodsClick(item)">
						{{ item }}
					</view>
				</view>
			</view>
		</template>
		<template v-else>
			<view v-if="!emptyShow">
				<view class="complist">
					<view class="index_sale">
						<view class="free-WaterfallFlow">
							<view class="flex-wrap" v-for="(item,index) in productList" :key="index"
								@click="jump(item.id)">
								<view class="page-column">
									<image mode="widthFix" :src="item.image"></image>
								</view>
								<view class="page-title text-cut">
									{{ item.name }}
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view v-else>
				<view class="empty">
					<view class="empty-text">
						<view class="">
							暂时没有找到你想拍的产品
						</view>
						<view class="">
							试试其它关键词吧
						</view>
					</view>
				</view>
			</view>
		</template>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				placeholderText: '请输入关键词',
				hotSearchList: [],
				isClear: false,
				productList: [],
				isSearch: false,
				keywords: '',
				emptyShow: false
			}
		},
		computed: {
			...mapGetters({
				historySearch: 'user/historySearch'
			})
		},
		onLoad() {
			this.getData()
		},
		methods: {
			...mapMutations({
				setHistorySearch: 'user/historySearch',
				clearHistorySearch: 'user/clearHistorySearch'
			}),
			getData() {
				let _this = this
				_this.$api.get('meet.index/search', {}).then(res => {
					_this.hotSearchList = res.data.hot_search
				}, fail => {})
			},
			getList() {
				let _this = this
				uni.showLoading({
					title: '搜索中...',
					mask: true
				})
				_this.$api.get('meet/product.index/search', {
					keywords: _this.keywords
				}).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						_this.productList = res.data.list
						if (_this.productList.length <= 0) {
							_this.emptyShow = true
						}
					}
				}, fail => {})
			},
			searchClick() {
				if (this.keywords) {
					this.setHistorySearch(this.keywords)
					this.isSearch = true

					this.getList()
				}
			},
			searchConfirm(e) {
				let _this = this
				if (e.value) {
					_this.setHistorySearch(e.value)
					_this.isSearch = true

					_this.getList()
				}
			},
			searchClear(e) {
				this.isSearch = false
				this.productList = []
				this.emptyShow = false
			},
			searchInput(e) {
				if (!e) {
					this.isSearch = false
					this.emptyShow = false
				}
			},
			clearupClick() {
				this.isClear = !this.isClear
			},
			clearAll() {
				let _this = this
				uni.showModal({
					title: '提示',
					content: '是否确认清除全部历史记录？',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确认',
					success: res => {
						if (res.confirm) {
							_this.clearHistorySearch({
								type: 1
							})
							_this.isClear = false
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			clearKeywords(index) {
				this.clearHistorySearch({
					type: 0,
					index: index
				})

				if (this.historySearch.length <= 0) {
					this.isClear = false
				}
			},
			keywrodsClick(keywords) {
				this.setHistorySearch(keywords)
				this.keywords = keywords
				this.isSearch = true
				this.isClear = false
				this.getList()
			},
			jump(id) {
				uni.navigateTo({
					url: '/pages/product/detail?id=' + id
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		padding-bottom: 50rpx;
	}

	.text-cut {
		overflow: hidden;
		-webkit-line-clamp: 1;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		text-overflow: ellipsis;
	}

	.search {
		display: flex;
		align-items: center;
		justify-content: space-between;

		.left {
			width: 90%;
		}

		.right {
			width: 10%;
			text-align: left;
			font-weight: 800;
		}
	}

	.history {
		margin-top: 20rpx;
		padding: 0 24rpx;

		.header {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.text {
				font-weight: 800;
				font-size: 32rpx;
				color: #000;
			}

			.clear {
				font-weight: 400;
				font-size: 24rpx;
				color: #8a8a8a;

				.clear-item {
					display: flex;
					align-items: center;

					.line {
						height: 25rpx;
						width: 2rpx;
						background-color: #bfbfbf;
						margin: 0 10rpx;
					}

					.complete {
						color: #35D14A;
					}
				}
			}
		}
	}

	.hot {
		margin-top: 50rpx;
		padding: 0 24rpx;

		.header {
			font-weight: 800;
			font-size: 32rpx;
			color: #000;
			text-align: left;
		}
	}

	.tags {
		display: flex;
		align-items: center;
		flex-wrap: wrap;
		margin-top: 30rpx;

		.tag-item {
			position: relative;

			.close {
				position: absolute;
				top: -20rpx;
				right: 10rpx;
			}
		}

		.tag {
			line-height: 14px;
			font-size: 12px;
			font-weight: 400;
			padding: 6px 20px;
			border-radius: 50rpx;
			border-width: 1px;
			border-style: solid;
			background-color: #fff;
			border-color: #bfbfbf;
			color: #000;
			margin-right: 20rpx;
			margin-bottom: 20rpx;
			position: relative;
		}
	}

	.complist {
		margin-top: 10px;
		padding: 0 15px;

		.index_sale {
			.free-WaterfallFlow {
				width: 100%;

				.flex-wrap {
					display: inline-block;
					width: 50%;
					text-align: left;
					vertical-align: top;
					margin-bottom: 24rpx;

					.page-column {
						width: 100%;
						// min-height: 506rpx;

						image {
							width: 97%;
							margin: 0 auto;
							border-radius: 14rpx;
							border: 2rpx solid #707070;
						}
					}

					.page-title {
						text-align: left;
						font-weight: 400;
						font-size: 28rpx;
					}
				}

				.flex-wrap:nth-child(2n) {
					text-align: right;
				}
			}
		}
	}

	.empty {
		width: 100%;
		text-align: center;
		margin-top: 40%;

		image {
			width: 200rpx;
			height: 200rpx;
		}

		.empty-text {
			font-size: 32rpx;
			color: #999999;
			margin-top: 80rpx;
		}
	}
</style>